package org.jeecg.demo.wx.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.demo.wx.aspect.WXTokenAuthAspect;
import org.jeecg.demo.wx.constant.CacheConstant;
import org.jeecg.demo.wx.convert.EScheduledTypeConvert;
import org.jeecg.demo.wx.entity.EScheduledType;
import org.jeecg.demo.wx.service.IEScheduledTypeService;
import org.jeecg.demo.wx.vo.EScheduledTypeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @Description: e_scheduled_type
 * @Author: jeecg-boot
 * @Date: 2024-01-30
 * @Version: V1.0
 */
@Api(tags = "e_scheduled_type")
@RestController
@RequestMapping("/wx/eScheduledType")
@Slf4j
public class EScheduledTypeController extends JeecgController<EScheduledType, IEScheduledTypeService> {
    @Autowired
    private IEScheduledTypeService eScheduledTypeService;
    @Autowired
    private RedisUtil redisUtil;

    /**
     * 分页列表查询
     *
     * @param eScheduledType
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "e_scheduled_type-分页列表查询")
    @ApiOperation(value = "e_scheduled_type-分页列表查询", notes = "e_scheduled_type-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<EScheduledType>> queryPageList(EScheduledType eScheduledType,
                                                       @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                       HttpServletRequest req) {
        QueryWrapper<EScheduledType> queryWrapper = QueryGenerator.initQueryWrapper(eScheduledType, req.getParameterMap());
        Page<EScheduledType> page = new Page<EScheduledType>(pageNo, pageSize);
        IPage<EScheduledType> pageList = eScheduledTypeService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    @GetMapping("/volist")
//    @WXTokenAuthAspect
    public Result<List<EScheduledTypeVo>> volist() {
        List<EScheduledTypeVo> cache = getCache();
        if (Objects.nonNull(cache) && !cache.isEmpty()) {
            return Result.ok(cache);
        }
        List<EScheduledType> eScheduledTypes = eScheduledTypeService
                .lambdaQuery()
                .select(EScheduledType::getName, EScheduledType::getId)
                .eq(EScheduledType::getDelFlag, 0)
                .list();
        List<EScheduledTypeVo> eScheduledTypeVos = EScheduledTypeConvert.INSTANCE.convertList(eScheduledTypes);
        redisUtil.set(CacheConstant.WX_SCHEDULED_TYPE_KEY, eScheduledTypeVos);
        return Result.ok(eScheduledTypeVos);
    }

    /**
     * 添加
     *
     * @param eScheduledType
     * @return
     */
    @AutoLog(value = "e_scheduled_type-添加")
    @ApiOperation(value = "e_scheduled_type-添加", notes = "e_scheduled_type-添加")
    @RequiresPermissions("wx:e_scheduled_type:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody EScheduledType eScheduledType) {
        eScheduledTypeService.save(eScheduledType);
        deleteCache();
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param eScheduledType
     * @return
     */
    @AutoLog(value = "e_scheduled_type-编辑")
    @ApiOperation(value = "e_scheduled_type-编辑", notes = "e_scheduled_type-编辑")
    @RequiresPermissions("wx:e_scheduled_type:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody EScheduledType eScheduledType) {
        eScheduledTypeService.updateById(eScheduledType);
        deleteCache();

        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "e_scheduled_type-通过id删除")
    @ApiOperation(value = "e_scheduled_type-通过id删除", notes = "e_scheduled_type-通过id删除")
    @RequiresPermissions("wx:e_scheduled_type:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        eScheduledTypeService.removeById(id);
        deleteCache();

        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "e_scheduled_type-批量删除")
    @ApiOperation(value = "e_scheduled_type-批量删除", notes = "e_scheduled_type-批量删除")
    @RequiresPermissions("wx:e_scheduled_type:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.eScheduledTypeService.removeByIds(Arrays.asList(ids.split(",")));
        deleteCache();
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "e_scheduled_type-通过id查询")
    @ApiOperation(value = "e_scheduled_type-通过id查询", notes = "e_scheduled_type-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<EScheduledType> queryById(@RequestParam(name = "id", required = true) String id) {
        EScheduledType eScheduledType = eScheduledTypeService.getById(id);
        if (eScheduledType == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(eScheduledType);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param eScheduledType
     */
    @RequiresPermissions("wx:e_scheduled_type:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, EScheduledType eScheduledType) {
        return super.exportXls(request, eScheduledType, EScheduledType.class, "e_scheduled_type");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequiresPermissions("wx:e_scheduled_type:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, EScheduledType.class);
    }

    private void deleteCache() {
        redisUtil.del(CacheConstant.WX_SCHEDULED_TYPE_KEY);
    }

    private List<EScheduledTypeVo> getCache() {
        return (List<EScheduledTypeVo>) redisUtil.get(CacheConstant.WX_SCHEDULED_TYPE_KEY);
    }

}
